# File mcf3.mod 
# Flow instance with explicit sources and destinations (sinks), 
# and capacity constraints 

param n;     # Number of nodes;

set SOURCES within {1..n};
set DESTINATIONS within {1..n};

set ARCS within {1..n, 1..n};

param supply {SOURCES} >= 0;
param demand {DESTINATIONS} >= 0;

check: sum {i in SOURCES} supply[i] = sum {i in DESTINATIONS} demand[i];

param cost {ARCS};
param u {ARCS};

var x {(i,j) in ARCS} >=0, <= u[i,j];

minimize total_cost:
    sum { (i,j) in ARCS } cost[i,j]*x[i,j];
subject to supp {i in SOURCES}:
sum{ (i,j) in ARCS } x[i,j] - sum { (j,i) in ARCS } x[j,i]   = supply[i];

subject to dem {i in DESTINATIONS}:
sum{ (i,j) in ARCS } x[i,j] - sum { (j,i) in ARCS } x[j,i]   = -demand[i];


subject to bal {i in {1..n} diff SOURCES diff DESTINATIONS}:
sum{ (i,j) in ARCS } x[i,j] - sum { (j,i) in ARCS } x[j,i]   = 0;
